package com.air.service.impl;

import com.air.config.AliPayConfig;
import com.air.dao.FlightMapper;
import com.air.dao.OrderMapper;
import com.air.entity.Flight;
import com.air.entity.Order;
import com.air.service.AliPayService;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class AliPayServiceImpl implements AliPayService {



    @Autowired
    OrderMapper orderMapper;

    @Autowired
    FlightMapper flightMapper;

    @Override
    public AlipayTradePagePayResponse pay(int orderId) throws Exception {
        Order order = orderMapper.getOrderByOrderId(orderId);
        if (order == null) return null;
        Flight flight = flightMapper.selecFlightById(String.valueOf(order.getFligthId()));
        if (flight == null) return null;
        Factory.setOptions(AliPayConfig.getOptions());

        AlipayTradePagePayResponse pay = Factory.Payment
                .Page()
                //.asyncNotify("https://www.test.com/callback")
                .pay(flight.getName(), String.valueOf(orderId), String.valueOf(order.getCost()), "http://localhost:8181/returnUrl");

        return pay;

    }


}
